package vsvm.graphics.charts;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Properties;

import vsvm.data.model.AbstractAttribute;
import vsvm.math.statistics.histogram.Histogram;
import vsvm.math.statistics.histogram.HistogramItem;
import vsvm.math.statistics.histogram.*;

public class HistogramChart extends AbstractChart{
	private static final long serialVersionUID = 100L;
	
	private Histogram histogram;
	private double fillFactor = 0.8;
	
	
	public HistogramChart(String title, Histogram histogram)
	{	
		super(title);
		this.title = title;
		this.histogram = histogram;		
	}
	
	protected void prepareColors() {
		this.setColors(new Color[]{Color.GREEN});
	}
	
	protected void prepareChart()
	{
		this.clear(0);
		int width  = this.getPlotRectangle().width;
		int height = this.getPlotRectangle().height;		
		double maxFreq 	  = histogram.getMaxFrequency();	
		int attributeType = histogram.getHistogramData().getAttributeType();
		int itemsCnt = histogram.getHistogramData().getItemsCount();
		
		if (attributeType == AbstractAttribute.NUMERIC_TYPE){
			this.setXRange(histogram.getHistogramData().getHistogramItem(0).getStart(), 
						   histogram.getHistogramData().getHistogramItem(itemsCnt - 1).getEnd());
		} else if (attributeType == AbstractAttribute.CATEGORIAL_TYPE){
			this.setXRange(0, itemsCnt + 1);
		}
		this.setYRange(0, maxFreq/fillFactor);
		
		this.setGrid(false);
		this.setTitle(title);		
		this.setBars(true);	
		if (attributeType == AbstractAttribute.NUMERIC_TYPE)
			this.setBars((histogram.getHistogramData().getHistogramItem(0).getEnd() - 
					      histogram.getHistogramData().getHistogramItem(0).getStart())*0.9, 0);
		
		
		for (int i = 0; i < itemsCnt; i++){			
			HistogramItem item = histogram.getHistogramData().getHistogramItem(i);
			if (attributeType == AbstractAttribute.NUMERIC_TYPE)
				this.addPoint(0, (item.getStart() + item.getEnd())/2, item.getFrequency(), false);
			else if (attributeType == AbstractAttribute.CATEGORIAL_TYPE)
			{
				this.addPoint(0, i + 1, item.getFrequency(), false);
				this.addXTick(item.getLabel(), i + 1);
			}
		}	
		
		chartPrepared = true;
	}
	
	protected Graphics2D drawChart(Graphics2D g)
	{		
		return g;
	}
	
	public Histogram getHistogram() {
		return this.histogram;
	}
	
	
	
}
